/********************
* top run 
* Create cohorts and tables
*
* Calls all programs/macros and runs results
*
* All inputs can be edited/saved here
*
*******************/

/* inputs */
%let programfolder = C:\Users\kcha0642\Documents\NSW-ON-NY\on-ny-nsw-kcha0642\on-ny-nsw; 
%let outputfolder = Z:\LowValueCare\APDC\on-ny-nsw; 

/* libraries */
libname apdc "Z:\LowValueCare\APDC\APDC 2019 Update";		* set to directory containing APDC dataset;
libname library "Z:\LowValueCare\APDC\APDC 2019 Update";	* set to directory containing formats library;
libname output "Z:\LowValueCare\APDC\on-ny-nsw";			* set to directory for final cohorts; 

/***********************/
/* macros and programs */
/***********************/
/* 1. procedure-codes */
* if procedure or diagnosis code requires editing, do this in the xlsx files. this creates
* the list of specific procedure and diagnosis codes for the cohorts; 
%include "&programfolder\procedure-codes.SAS"; 

/* 2. get-cohorts */
%include "&programfolder\get-cohort.SAS" / lrecl=500;

/* 3. add-seq-vars */
%include "&programfolder\add-seq-vars-2.SAS" / lrecl=500;

/* 4. exclusions-cohort */ 
%include "&programfolder\exclusions-cohort.SAS" / lrecl = 500; 

/* 5. elixhauser */ 
%include "&programfolder\elixhauser.SAS" / lrecl=500;

/* 6. admission-vairables */ 
%include "&programfolder\admission-variables-table4-only.SAS" / lrecl=500; 

%include "&programfolder\sa2_2016_conversion.SAS" / lrecl=500; 

%include "&programfolder\results-table1.SAS" / lrecl=500; 

* age & sex standardised rates for table 2 (per 100,000 population per-year); 
%include "&programfolder\results-table2.SAS" / lrecl=500; 

* age & sex standardised utilisation by neighbourhood income quintile; 
%include "&programfolder\results-table3.SAS" / lrecl=500; 

/**************************/
/* procedure cohorts	 */ 
/**************************/
/* inputs */ 
%let firstdateval = '1jan2013'd;
%let lastdateval = '31mar2018'd; 
%let periodval = 5; 

/* *************************/
/* pancreatectomy */ 
%get_cohort(indata = apdc.apdcx, outdata = pancreat, proc_codes = &pancreatectomy);
%makeseqvars(indata = pancreat, outdata = pancreat, firstdate = &firstdateval); 

ods html file="exclusions_pancreatectomy.html" path="&outputfolder\";
%exclusions(indata = pancreat, outdata = output.pancreatcohort, 
			label = pancreatectomy, firstdate = &firstdateval, lastdate = &lastdateval, period = &periodval); 

%elix(infile=output.pancreatcohort, outfile=output.pancreatcohort);
ods html file="variables_pancreatectomy.html" path="&outputfolder\";
%admissionvariables(cohortdata = output.pancreatcohort, allepisodes = pancreat, outdata = output.pancreatcohort); 

ods html file="table1_pancreatectomy.html" path="&outputfolder\";
%tableone(output.pancreatcohort, output.hospitalpancreat); 
ods html file="table2_pancreatectomy.html" path="&outputfolder\"; 
%table_two(output.pancreatcohort, codes.population_nsw, codes.population_ontario, label = pancreatectomy);
ods html file="table3_pancreatectomy.html" path="&outputfolder\"; 
%table_three(output.pancreatcohort, codes.population_nsw, codes.population_on_income, label = pancreatectomy);

/**************************/
/* prostatectomy */ 
%get_cohort(indata = apdc.apdcx, outdata = prostat , proc_codes = &prostatectomy);
%makeseqvars(indata = prostat, outdata = prostat, firstdate = &firstdateval); 

ods html file="exclusions_prostatectomy.html" path="&outputfolder\";
%exclusions(indata = output.prostatall, outdata = output.prostatcohort, 
			label = prostatectomy, firstdate = &firstdateval, lastdate = &lastdateval, period = &periodval); 

%elix(infile=output.prostatcohort, outfile=output.prostatcohort);
ods html file="variables_prostatectomy.html" path="&outputfolder\";
%admissionvariables(cohortdata = output.prostatcohort, allepisodes = output.prostatall, outdata = output.prostatcohort); 

ods html file="table1_prostat.html" path="&outputfolder\";
%tableone(output.prostatcohort, output.hospitalprostat); 
ods html file="table2_prostat.html" path="&outputfolder\"; 
%table_two(output.prostatcohort, codes.population_nsw, codes.population_ontario, label = prostatectomy);
ods html file="table3_prostat.html" path="&outputfolder\"; 
%table_three(output.prostatcohort, codes.population_nsw, codes.population_on_income,  label = prostatectomy);

/**************************/
/* nephrectomy */
%get_cohort(indata = apdc.apdcx, outdata = neph, proc_codes = &nephrectomy);
%makeseqvars(indata = neph, outdata = neph, firstdate = &firstdateval); 

ods html file="exclusions_nephrectomy.html" path="&outputfolder\";
%exclusions(indata = output.nephall, outdata = output.nephcohort, 
			label = nephrectomy, firstdate = &firstdateval, lastdate = &lastdateval, period = &periodval); 

%elix(infile=output.nephcohort, outfile=output.nephcohort);
ods html file="variables_nephrectomy.html" path="&outputfolder\";
%admissionvariables(cohortdata = output.nephcohort, allepisodes = neph, outdata = output.nephcohort); 

ods html file="table1_neph.html" path="&outputfolder\";
%tableone(output.nephcohort, output.hospitalneph); 
ods html file="table2_neph.html" path="&outputfolder\"; 
%table_two(output.nephcohort, codes.population_nsw, codes.population_ontario,label = nephrectomy);
ods html file="table3_neph.html" path="&outputfolder\"; 
%table_three(output.nephcohort, codes.population_nsw, codes.population_on_income,label = nephrectomy);